#!/usr/bin/python3.7
# -*- coding: utf-8 -*-
# @Time    : 2019/7/14 23:50
# @Email  : jtyoui@qq.com
# @Software: PyCharm
import numpy
from paddle import fluid
from PIL import Image


def load_image(file):
    im = Image.open(file).convert('L')
    im = im.resize((28, 28), Image.ANTIALIAS)
    im = numpy.array(im).reshape((1, 1, 28, 28)).astype(numpy.float32)
    im = im / 255.0 * 2.0 - 1.0
    return im


model = './model'

tensor_img = load_image('./image/9.jpg')
place = fluid.CPUPlace()
exe = fluid.Executor(place=place)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
    # feed_target_names 用于指定需要传入网络的变量名
    # fetch_targets 指定希望从网络中fetch出的变量名
    inference_program, feed_target_names, fetch_targets = fluid.io.load_inference_model(model, exe, None, None)
    results = exe.run(program=inference_program,
                      feed={feed_target_names[0]: tensor_img},
                      fetch_list=fetch_targets)
    for number, acc in enumerate(results[0][0]):
        print(f'是{number}的概率为：{acc * 100:.5f}%')
